package com.example.user.user

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.example.library_base.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(
    private val repository: UserRepository,
    private val savedStateHandle: SavedStateHandle
): BaseViewModel<UserModel>() {
    private val timer = Timer()
    private var m = 1

//    fun getArticle() = liveData {
//        println("===========网络请求")
//        repository.getArticle()
//            .catch { e->
//                emit(HhResult.Failure(e.message!!, e))
//            }
//            .collect { it ->
//                emit(HhResult.Success(it))
//            }
//    }

//    fun getArticle() = liveData<Any>{
//        launch({
//            val map = mutableMapOf<String, Any>()
//            map["city_id"] = "f4725cc0-26bd-485d-986d-f6837e2a9c77"
//            println("======网络请求开始")
//            repository.getRestaurantList(map)
//        },{
//            println("======网络请求错误")
//            println(it.message)
//        },{
//            println("======网络请求结束")
//        })
//    }
//
//    private fun launch(block: suspend ()->Unit, error: suspend (Throwable) -> Unit, complete: suspend () -> Unit) =
//            viewModelScope.launch {
//                try {
//                    block()
//                }catch (e: Throwable){
//                    error(e)
//                }finally {
//                    complete()
//                }
//            }

    fun getRestaurantList(){
        val map = mutableMapOf<String, Any>()
        map["city_id"] = "f4725cc0-26bd-485d-986d-f6837e2a9c77"
        println("===========${map["city_id"]}")
        viewModelScope.launch {
            val s = repository.getRestaurantList(map)
            println("=============网络请求返回值==$s")
        }
     }

    fun getData(){
        println("===============net start")
        val task: TimerTask = object : TimerTask() {
            override fun run() {
                println("===========run start")
                val userModel = UserModel()
                userModel.name = "网络请求数据"
                userModel.isClick = true
                setModelLiveData(userModel)
                println("===========run end")
            }
        }
        val timer = Timer()
        timer.schedule(task, 2000)
        println("===============net start")
    }

    fun startTime(){
        println("===============start time")
        println("=============== ${m++}")
        val task: TimerTask = object : TimerTask() {
            override fun run() {
                println("===========run start")
                getModelLiveData().value?.let {
                    it.timeCount ++
                    setModelLiveData(it)
                }
                println("===========run end")
            }
        }
        timer.schedule(task, 1000, 1000)
        println("===============end time")
    }

    override fun onCleared() {
        super.onCleared()
        timer.cancel()
    }
}